/*
 * @Description: 首页HomePage
 */
import 'package:flutter/material.dart';
import 'package:C20Driver/pages/index_page/home_page/app_home_page.dart';
import 'package:C20Driver/pages/index_page/home_page/unregistered_home.dart';
import 'package:C20Driver/service/mqtt_manage.dart';
import 'package:C20Driver/service/request.dart';
import 'package:C20Driver/widget/spinkit.dart';
import 'package:provider/provider.dart';
import 'package:C20Driver/provider/driver_information.dart';
import 'package:async/async.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with AutomaticKeepAliveClientMixin {
  final AsyncMemoizer _memoizer = AsyncMemoizer();
  var clientId;
  // 获取司机信息
  Future _getDriverInfo(BuildContext context) async {
    return _memoizer.runOnce(() async {
      return await Request.getDriverInfoFun(context);
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    clientId = MqttManage.getInstance().getClientId;
    print("clientid$clientId");
  }

  // @override
  // Widget build(BuildContext context) {
  //   super.build(context);
  //   return Container(color: Colors.white, child: UnregisteredHome());
  // }
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return FutureBuilder(
      future: _getDriverInfo(context),
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        if (snapshot.hasData) {
          return Container(
              color: Colors.white,
              child: Consumer<DriverInformation>(builder: (ctx, state, child) {
                return Stack(
                  children: <Widget>[
                    Offstage(
                      offstage: state.driverInfo.authenticationStatus != '0000',
                      child: AppHomePage(),
                    ),
                    Offstage(
                      offstage: state.driverInfo.authenticationStatus == '0000',
                      child: UnregisteredHome(),
                    ),
                  ],
                );
              }));
        } else {
          return Spinkit();
        }
      },
    );
  }

  @override
  bool get wantKeepAlive => true;
}
